package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pojo.Customer;
import util.DBUtil;
import dao.CustomerDao;

public class CustomerDaoImpl implements CustomerDao {

	public int addCustomer(Customer c) {
		String sql = "insert into customers(id,cname,sex,sal,birth) values(seq01.nextval,?,?,?,?)";
		Connection con=null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, c.getCname());
			pst.setBoolean(2, c.isSex());
			pst.setDouble(3, c.getSal());
			pst.setDate(4, new java.sql.Date(c.getBirth().getTime()));
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pst, null);
		}
		return m;
	}

	public int deleteCustomer(Integer id) {
		String sql = "delete customers where id=?";
		Connection con=null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, id);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pst, null);
		}
		return m;
	}

	public int updateCustomer(Customer c) {
		String sql = "update customers set cname=?,sex=?,sal=?,birth=?  where id=?";
		Connection con=null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, c.getCname());
			pst.setBoolean(2, c.isSex());
			pst.setDouble(3, c.getSal());
			pst.setDate(4, new java.sql.Date(c.getBirth().getTime()));
			pst.setInt(5, c.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pst, null);
		}
		return m;
	}

	public Customer loadCustomer(Integer id) {
		Customer c=null;
		String sql = "select * from customers where id=?";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, id);;
			rs = pst.executeQuery();
			if (rs.next()) {
				c=new Customer();
				String cname = rs.getString("cname");
				
				double sal = rs.getDouble("sal");
				boolean sex = rs.getBoolean("sex");
				Date birth = new Date(rs.getDate("birth").getTime());
				c.setId(id);c.setCname(cname);c.setBirth(birth);c.setSex(sex);
				c.setSal(sal);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return c;
	}

	public List<Customer> findAll() {
		List<Customer> list=new ArrayList<Customer>();
		String sql = "select * from customers";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			
			rs = pst.executeQuery();
			if (rs.next()) {
				Customer c=new Customer();
				int id=rs.getInt("id");
				String cname = rs.getString("cname");
				double sal = rs.getDouble("sal");
				boolean sex = rs.getBoolean("sex");
				Date birth = new Date(rs.getDate("birth").getTime());
				c.setId(id);c.setCname(cname);c.setBirth(birth);c.setSex(sex);
				c.setSal(sal);
				list.add(c);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return list;
	}

}
